<?php
/**
 * @file
 * Creates user interface for adding data to the organizations table.
 * @version 8/9/11
 * @author Matt Vartanian
 */

include_once ('../db-control/dbDonor.php');
include_once ('../db-control/dbOrganization.php');
include_once ('header.php');
// array of POST data
addOrganization();

/**
 * @function addOrganization
 * Calls all other functions and creates addOrganization.php UI. Initializes
 * $inputData array which holds $_POST information and $errorArray which holds
 * error information pertaining to each field in the form.
 */
function addOrganization()
{
	$inputData = array('organizationName'=>"", 'description'=>"",'street'=>"",
    'city'=>"", 'zip'=>"", 'phone'=>"", 'fax'=>"", 'email'=>"");

	$errorArray = array('organizationError'=>"", 'descriptionError'=>"",
    'streetError'=>"", 'cityError'=>"",'zipError'=>"",'phoneError'=>"",
    'faxError'=>"",'emailError'=>"");
	checkEnterOrganizationButton($inputData, $errorArray);
	checkClearFieldsButton();
	viewAddOrganizationForm($inputData, $errorArray);
}

/**
 * @function checkEnterOrganizationButton
 * Checks to see if the enter organization button has been pressed.
 * Performs error * checking and requires all fields to be filled in before
 * submitting data to * database.
 * @param array $inputData array of $_POST data
 * @param string $donorName name of newly entered individual
 */
function checkEnterOrganizationButton(&$inputData, &$errorArray)
{
	$allFieldsFilled=true;
	$id = 0;
	if (isset($_POST['enterDonor'])) {
		areFieldsFilled($allFieldsFilled, $errorArray);

		if($allFieldsFilled == true) {
			getPostVars($inputData);

			dbDonor::addDonorByFields($inputData['street'], $inputData['city'],
			$inputData['state'], $inputData['zip'], $inputData['phone'],
			$inputData['fax'], $inputData['email'], $id);

			dbOrganization::addOrganizationByFields($id,
			$inputData['organizationName'],	$inputData['description'], $id);
			$donorName = $inputData['organizationName'];

			echo $inputData['organizationName']."
			has been entered into the database.";
			dbOrganization::verifyInsertedOrganization($id);
		}
		else { // submit was pressed, but some fields are empty
			echo "<font color=FF0000> * indicates required fields,
			please re-enter. <br /></font>";
			getPostVars($inputData);
		}
	}
}

/** @function checkClearFieldsButton
 * Checks to see if the Clear Fields button has been pressed. If it has been
 * pressed, resets page.
 */
Function checkClearFieldsButton()
{
	if (isset($_POST['clearFields'])) {
	}
}

/**
 * Creates the form for entering organization data.
 *
 * @param $inputdata is an array of $_POST variables entered by the user.
 * @param $organizationError records whether the corresponding field has data
 * entered in it or not.
 *
 */
function viewAddOrganizationForm($inputData, $errorArray)
{
	echo <<<_END
  <form method="post" action="addOrganization.php"><pre>
    Organization Name  <input type="text" name="organizationName" 
    value="$inputData[organizationName]"/> {$errorArray['organizationError']}

    Enter Description  <textarea name="description">$inputData[description]</textarea>{$errorArray['descriptionError']}
    Street             <input type="text" name="street" 
    value="$inputData[street]"/> {$errorArray['streetError']}
    City               <input type="text" name="city" 
    value="$inputData[city]"/> {$errorArray['cityError']}
    <br />
_END;
    state();
    echo <<<_END
    Zip     <input type="text" size="8" maxlength="8" name="zip" 
    value="$inputData[zip]"/> {$errorArray['zipError']}
    Phone              <input type="text" name="phone" size="32" 
    value="$inputData[phone]"/> {$errorArray['phoneError']}
    Fax                <input type="text" name="fax" size="32" 
    value="$inputData[fax]"/> {$errorArray['faxError']}
    E-mail             <input type="text" name="email" size="32" 
    value="$inputData[email]"/> {$errorArray['emailError']}</pre>
    <input type="submit" value="Submit" name="enterDonor" />  
    <input type="submit" value="Clear Fields" name="clearFields" />  
  </form>
_END;
}

/**
 * Retrieves the data entered by the user into the fields represented.
 *
 * @param $inputData is an array of $_POST variables entered by the user.
 */
function getPostVars(&$inputData)
{
	extract($_POST);
	$inputData = compact('organizationName', 'description', 'street', 'city',
	'state', 'zip', 'phone', 'fax', 'email');
}

/**
 * Checks that all fields have values entered. If there are empty fields,
 * corresponding error variables are assigned value of "*".
 *    *
 * @param $allFieldsFilled is a boolean value which determines whether or not
 * the user has neglected to enter data into any of the available fields.
 * @param $errorArray is an array passed by reference which is set to
 * "*" if the user has not entered any data into the corresponding field.
 */
function areFieldsFilled(&$allFieldsFilled, &$errorArray)
{
	if(trim($_POST['organizationName']) == ""){
		$errorArray['organizationError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['description'])== ""){
		$errorArray['descriptionError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['street'])== ""){
		$errorArray['streetError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['city'])== ""){
		$errorArray['cityError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['zip'])== ""){
		$errorArray['zipError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['phone'])== ""){
		$errorArray['phoneError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['fax'])== ""){
		$errorArray['faxError']="*";
		$allFieldsFilled=false;
	}
	if(trim($_POST['email'])== ""){
		$errorArray['emailError']="*";
		$allFieldsFilled=false;
	}

}

/**
 * Creates a list box for state selection.
 */
function state()
{
	echo <<<_END
    State Name         <select name="state">
        <option value="AL">AL</option>
        <option value="AK">AK</option>
        <option value="AZ">AZ</option>
        <option value="AR">AR</option>
        <option value="CA">CA</option>
        <option value="CO">CO</option>
        <option value="CT">CT</option>
        <option value="DE">DE</option>
        <option value="DC">DC</option>
        <option value="FL">FL</option>
        <option value="GA">GA</option>
        <option value="HI">HI</option>
        <option value="ID">ID</option>
        <option value="IL">IL</option>
        <option value="IN">IN</option>
        <option value="IA">IA</option>
        <option value="KS">KS</option>
        <option value="KY">KY</option>
        <option value="LA">LA</option>
        <option value="ME">ME</option>
        <option value="MD">MD</option>
        <option value="MA">MA</option>
        <option value="MI">MI</option>
        <option value="MN">MN</option>
        <option value="MS">MS</option>
        <option value="MO">MO</option>
        <option value="MT">MT</option>
        <option value="NE">NE</option>
        <option value="NV">NV</option>
        <option value="NH">NH</option>
        <option value="NJ">NJ</option>
        <option value="NM">NM</option>
        <option value="NY">NY</option>
        <option value="NC">NC</option>
        <option value="ND">ND</option>
        <option value="OH">OH</option>
        <option value="OK">OK</option>
        <option value="OR">OR</option>
        <option value="PA">PA</option>
        <option value="RI">RI</option>
        <option value="SC">SC</option>
        <option value="SD">SD</option>
        <option value="TN">TN</option>
        <option value="TX">TX</option>
        <option value="UT">UT</option>
        <option value="VT">VT</option>
        <option value="VA">VA</option>
        <option value="WA">WA</option>
        <option value="WV">WV</option>
        <option value="WI">WI</option>
        <option value="WY">WY</option>
        </select>
_END;
}

?>